package com.hwpt.controller.admin;



import com.hwpt.dto.admin.BatchDeleteRequest;
import com.hwpt.dto.admin.UserBasicInfoDTO;
import com.hwpt.dto.admin.UserListDTO;
import com.hwpt.entity.UserBasicInfo;
import com.hwpt.mapper.UserBasicInfoMapper;
import com.hwpt.mapper.UserRealNameAuthMapper;
import com.hwpt.result.PageResult;
import com.hwpt.result.Result;
import com.hwpt.service.UserInfoService;
import com.hwpt.vo.admin.UserBasicInfoVo;
import com.hwpt.vo.admin.UserListVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/admin")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "用户信息管理接口")
@Controller("adminUserInfoController")
public class UserInfoController {

    private final UserInfoService userInfoService;

    @PostMapping("/userInfo/list")
    @ApiOperation("获取用户列表：分页查询")
    public Result<PageResult<UserListVo>> list(
            @ApiParam("分页查询用户列表参数") UserListDTO userListDTO) {
        PageResult<UserListVo> pageResult = userInfoService.list(userListDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/userInfo/getById/{userId}")
    @ApiOperation("获取用户详细信息")
    public Result<UserBasicInfoVo> getUser(@PathVariable("userId") String userId) {
        return Result.success(userInfoService.getUser(userId));
    }

    @PostMapping("/userInfo/addUser")
    @ApiOperation("添加用户")
    public Result<Map<String, Long>> addUser(@RequestBody UserBasicInfo userBasicInfo) {
        log.info("添加用户请求: {}", userBasicInfo);
        Long userId = userInfoService.addUser(userBasicInfo);
        Map<String, Long> responseData = new HashMap<>();
        responseData.put("userId", userId);
        return Result.success(responseData);
    }

    @PutMapping("/userInfo/update/{userId}")
    @ApiOperation("更新用户信息")
    public Result<String> updateUser(@PathVariable("userId") String userId, @RequestBody UserBasicInfoDTO userBasicInfoDTO) {
        log.info("更新用户信息请求，用户ID: {}, 请求参数: {}", userId, userBasicInfoDTO);
        userInfoService.updateUser(userId, userBasicInfoDTO);
        return Result.success("用户信息更新成功");
    }

    @DeleteMapping("/userInfo/delete/{userId}")
    @ApiOperation("删除用户")
    public Result<String> deleteUser(@PathVariable("userId") String userId) {
        log.info("删除用户请求，用户ID: {}", userId);
        userInfoService.deleteUser(userId);
        return Result.success("用户删除成功");
    }

    @PostMapping("/users/batch-delete")
    @ApiOperation("批量删除用户")
    public Result<String> batchDeleteUsers(@RequestBody @Valid BatchDeleteRequest batchDeleteRequest) {
        log.info("批量删除用户请求，用户ID列表: {}", batchDeleteRequest.getUserIds());
        userInfoService.batchDeleteUsers(batchDeleteRequest.getUserIds());
        return Result.success("批量删除成功");
    }
}